Page({
  data: {
    statusBarHeight: 0, // 状态栏高度
    addressList: [] // 初始化为空数组
  },

  onLoad: function (options) {
    // 获取状态栏高度
    const windowInfo = wx.getWindowInfo();
    this.setData({
      statusBarHeight: windowInfo.statusBarHeight
    });

    // 调用接口获取地址信息
    this.getAddressList();
  },

  // 获取地址列表
  getAddressList() {
    const app = getApp();
    const token = app.globalData.userInfo.token;
    const userId = app.globalData.userInfo.id;
    wx.request({
      url: `http://localhost:8080/manage/address/list`, // 替换为实际接口地址
      method: 'GET',
      data : { userId: userId },
      header: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      },
      
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          this.setData({
            addressList: res.data.rows // 更新地址列表
          });
        } else {
          wx.showToast({
            title: '获取地址失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      }
    });
  },

  // 返回按钮点击事件
  onBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },

  // 跳转到新增地址页面
  addAddress() {
    wx.navigateTo({
      url: '/pages/address-add/address-add'
    });
  },

  // 编辑地址
  editAddress(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/address-edit/address-edit?addressId=${id}`
    });
  }
});